/*
 * Middleware
 * Copyright (C) 2010/11
 * Riccardo Pulvirenti, Giuseppe Ravidà, Andrea Tino
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses
 */

/*!
 * File: worker_network.hpp
 */

#ifndef _WORKER_NETWORK_HPP_
#define _WORKER_NETWORK_HPP_

#include <iostream>
#include <vector>
#include <string>

#include "address.hpp"

//-----------------------------------------------------------------------------
// Enum, struct, aliases

//-----------------------------------------------------------------------------
// Class definitions
namespace middleware {
/*!
 * Represent a minimum collection of workers able to supply a particular
 * task to be executed.
 */
class WorkerNetwork {
public:
	typedef std::allocator< Address > Allocator;
	typedef std::vector< Address >::iterator AddressCollectionIterator;
	typedef std::vector< Address >::const_iterator
			AddressCollectionConstIterator;
	typedef unsigned int SizeType;
	typedef Allocator::reference Reference;
	typedef Allocator::const_reference ConstReference;
	/*!
	 * Constructs a WorkerNetwork with no worker inside.
	 */
	WorkerNetwork();
	/*!
	 * Copy constructor.
	 */
	WorkerNetwork(const WorkerNetwork& wn);
	/*!
	 * Destructor.
	 */
	~WorkerNetwork();
	/*!
	 * Gets iterator to addresses starting from top.
	 */
	AddressCollectionIterator begin() {
		return this->_workers.begin();
	}
	/*!
	 * Gets const iterator to addresses starting from top.
	 */
	AddressCollectionConstIterator begin() const {
		return this->_workers.begin();
	}
	/*!
	 * Gets iterator to addresses starting from bottom.
	 */
	AddressCollectionIterator end() {
		return this->_workers.end();
	}
	/*!
	 * Gets const iterator to addresses starting from bottom.
	 */
	AddressCollectionConstIterator end() const {
		return this->_workers.end();
	}
	/*!
	 * Causes the collection of addresses to be totally overwritten with new
	 * elements in the passed collection iterators. A copy of passed
	 * elements is performed. An stl container is preferred and Address type
	 * must be provided for elements in passed container.
	 */
	void assign(AddressCollectionConstIterator first,
			AddressCollectionConstIterator last);
	/*!
	 * Pushes a new address to the bottom of the collection.
	 */
	void push_back(ConstReference addr);
	/*!
	 * Accessor by position. Gets the reference.
	 */
	Reference operator[](SizeType position);
	/*!
	 * Accessor by position. Gets the const reference.
	 */
	ConstReference operator[](SizeType position) const;
	/*!
	 * Clears collection
	 */
	void clear();
	/*!
	 * Returns size
	 */
	SizeType size() const;
	/*!
	 * Tells whether the collection is empty.
	 */
	bool empty() const;
private:
	std::vector< Address > _workers;
};
} /* middleware */

#endif
